package tools;
import java.io.*;
//lineReader类中的工具可以帮助读取单个文本中某一特定行的内容
public class lineReader{
    //readLine函数能帮助读取文本中特定行的内容，括号中添加“文本路径”以及“查询关键字”，该函数返回关键字所在的那一行全部的“信息”
    public static String readLine(String txtPath,String idNum){
        String result=null;
        try{
            FileReader fr =new FileReader(new File(txtPath));
            BufferedReader br = new BufferedReader(fr);
            String line = br.readLine();
            while(line!=null){
                if(line.contains(idNum)){
                    result=line;
                    break;
                }else{
                    line=br.readLine();
                }
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return result;
    }

    //readLine_by_number函数能读取该文本中具体行数的内容，括号中添加“文本路径”以及“查询内容所在行数”，该函数返回关键字所在的那一行全部的“信息”
    public static String readLine_by_number(String txtPath,int lineNum){
        String result=null;
        try{
            FileReader fr =new FileReader(new File(txtPath));
            BufferedReader br = new BufferedReader(fr);
            String line = br.readLine();
            int i = 1;
            while(line!=null){
                if(i==lineNum){
                    result=line;
                    break;
                }else{
                    line=br.readLine();
                    i=i+1;
                }
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return result;
    }

    //findLine函数能帮助锁定某一信息所在的行数，括号中添加“文件路径”以及“查询关键字”，该函数返回关键字所在的“行数”
    public static int findLine(String txtPath,String idNum){
        int num=1;
        try{
            FileReader fr =new FileReader(new File(txtPath));
            BufferedReader br = new BufferedReader(fr);
            String line = br.readLine();
            while(line!=null){
                if(line.contains(idNum)){
                    break;
                }else{
                    num=num+1;
                    line=br.readLine();
                }
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return num;
    }
}